import { _decorator, assetManager, Component, ImageAsset, Label, Node, Sprite, SpriteFrame, Texture2D } from 'cc';
import { levelData } from './Rank';
import { Tools } from '../tools/Tools';
const { ccclass, property } = _decorator;

@ccclass('RankLevelItem')
export class RankLevelItem extends Component {
    @property(Sprite)
    mSpIcon: Sprite = null;
    @property(SpriteFrame)
    mDefultSpf: SpriteFrame = null;

    private mData = null;
    start() {

    }

    InitItem(data: levelData, index: number) {
        this.mData = data;
        let node = this.node;
        node.active = true;
        node.getChildByName("ranNum").getComponent(Label).string = (index + 1) + "";
        node.getChildByName("levelNum").getComponent(Label).string = Tools.FormatNumber(data.level);
        node.getChildByName("useTime").getComponent(Label).string = data.time.toFixed(2);
        node.getChildByName("name").getComponent(Label).string = data.nickName;
        if (data.headImg != "") {
            assetManager.loadRemote(data.headImg, { ext: '.png' }, (error, img: ImageAsset) => {
                if (!this.mSpIcon) return;
                if (!error && img) {
                    const texture = new Texture2D();
                    texture.image = img;
                    let spFrame = new SpriteFrame();
                    spFrame.texture = texture;
                    this.mSpIcon.spriteFrame = spFrame;
                }
            })
        } else {
            this.mSpIcon.spriteFrame = this.mDefultSpf;
        }
    }
}


